package com.tanhua.server.controller;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 根据环信用户id，查询用户信息
     *
     * @param huanxinId
     * @return
     */
    @GetMapping("/userinfo")
    public ResponseEntity userInfo(String huanxinId) {
        UserInfoVo vo = messagesService.findByUserInfoHuanxin(huanxinId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 添加联系人
     *
     * @param map
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        //从map集合中获取id 目前类型String
        String StrUserId = map.get("userId").toString();
        //将string类型的id转换为Long类型
        Long friendId = Long.valueOf(StrUserId);
        //调用方法添加联系人
        messagesService.addContacts(friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询好友列表
     *
     * @return
     */
    @GetMapping("/contacts")
    public ResponseEntity findAllContacts(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize,
                                          String keyword) {
        PageResult pr = messagesService.fandAllcontacts(page, pagesize, keyword);
        return ResponseEntity.ok(pr);
    }

    /**
     * 点赞列表
     *
     * @return
     */
    @GetMapping("/likes")
    public ResponseEntity findLikes(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = messagesService.findLikes(page, pagesize, CommentType.LIKE);
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论列表
     *
     * @return
     */
    @GetMapping("/comments")
    public ResponseEntity findcomments(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = messagesService.findAllComments(page, pagesize,CommentType.COMMENT);
        return ResponseEntity.ok(pr);
    }

    /**
     * 喜欢列表
     *
     * @return
     */
    @GetMapping("/loves")
    public ResponseEntity findLoves(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = messagesService.findLoves(page, pagesize,CommentType.LOVE);
        return ResponseEntity.ok(pr);
    }
}
